package com.kele.target22;

import java.util.concurrent.TimeUnit;

/**
 * @author chunhui.li
 * @date 2022/2/7 14:25
 * @Description 一定会死锁的案例
 * @ClassName DeadLockTest
 */
public class DeadLockTest {
    public static Object a = new Object();
    public static Object b = new Object();

    public static void main(String[] args) {
        new Thread(()->{
            System.out.println("线程A开始执行了");
            synchronized (a) {
                try {
                    System.out.println("线程A拿到锁a了");
                    TimeUnit.SECONDS.sleep(2);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                synchronized (b){
                    System.out.println("线程A拿到锁b了");
                }
            }
        },"A").start();

        new Thread(()->{
            System.out.println("线程B开始执行了");
            synchronized (b) {
                try {
                    System.out.println("线程B拿到锁b了");
                    TimeUnit.SECONDS.sleep(2);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                synchronized (a){
                    System.out.println("线程B拿到锁a了");
                }
            }
        },"B").start();
    }
}
